#coding=utf-8
from storm.locals import *
import decimal
import datetime

class User(object):
    __storm_table__ = 'users'
    id = Int(primary=True)
    name = Unicode()
    password = Unicode()
    
class Campaign(object):
    __storm_table__ = 'campaigns'
    id = Int(primary=True)
    user_id = Int()
    
    name = Unicode()
    description = Unicode()
    budget = Decimal()
    start_on = Date()
    end_on = Date()
    
    status = Unicode()
    created_at = DateTime()
    last_modified_at = DateTime()
    
    def get_sid(self):
        return "camp_%i"%self.id
    sid = property(fget=get_sid)
    
User.campaigns = ReferenceSet(User.id, Campaign.user_id)
Campaign.user = ReferenceSet(Campaign.user_id, User.id)

class Ad(object):
    __storm_table__ = 'ads'
    id = Int(primary=True)
    
    user_id = Int()
    campaign_id = Int()
    
    name = Unicode()
    description = Unicode()
    budget = Decimal()
    
    headline = Unicode()
    desc_line1 = Unicode()
    desc_line2 = Unicode()
    display_url = Unicode()
    destination_url = Unicode()
    
    status = Unicode()
    created_at = DateTime()
    last_modified_at = DateTime()
    
    def get_sid(self):
        return "ad_%i"%self.id
    sid = property(fget=get_sid)
    
    def toHtml(self):
        return '''
        <table border="0" cellpadding="2" cellspacing="0" width="97%%">
        <tr valign="bottom"><td valign="middle" style="width:1%%;">
        <table width="150" cellspacing="0" border="0">
        <tr><td bgcolor="#b4d0dc" style="border: none; white-space: nowrap" valign="middle" nowrap><table width="100%%" cellspacing="0" cellpadding="3" border="0"><tr><td align="left" bgcolor="#ffffff" style="border: none; white-space: nowrap" nowrap><a href=%s target="_blank"><font size="+0">%s</font></a> 
        <br> 
        %s
        <br> 
        %s
        <br> 
        <font color="green" title=%s>%s</font> 
        <br> 
        </td></tr></table>
        '''%(self.destination_url, self.headline, self.desc_line1, self.desc_line2, self.display_url,self.display_url)

    def keywords2str(self, type_='n'):
        keys = []
        for key in self.keywords:
            if key.is_negative == type_:
                keys.append(key.name)
        return ','.join(keys) + ','
        
User.ads = ReferenceSet(User.id, Ad.user_id)
Campaign.ads = ReferenceSet(Campaign.id, Ad.campaign_id)
Ad.user = ReferenceSet(Ad.user_id, User.id)
Ad.campaign = ReferenceSet(Ad.campaign_id, Campaign.id)

class Keyword(object):
    __storm_table__ = 'keywords'
    id = Int(primary=True)
    
    name = Unicode()
    bid = Decimal()
    
    is_negative = Unicode()
    
    created_at = DateTime()
    def __init__(self, name, is_negative=u'n'):
        self.name = name
        self.is_negative = is_negative
        
class AdKeyword(object):
    __storm_table__ = 'keyword_vs_ad'
    __storm_primary__ = 'keyword_id', 'ad_id'
    keyword_id = Int()
    ad_id = Int()

Ad.keywords = ReferenceSet(Ad.id,
                           AdKeyword.ad_id,
                           AdKeyword.keyword_id,
                           Keyword.id)
                           
Keyword.Ads = ReferenceSet(Keyword.id,
                           AdKeyword.keyword_id,
                           AdKeyword.ad_id,
                           Ad.id)


class ShowRecord(object):
    __storm_table__ = 'show_records'
    id = Int(primary=True)
    
    ad_id = Int()
    keyword_id = Int()
    
    showed_at = DateTime()
    show_cost = Decimal()
    
    ip = Unicode()
Ad.show_records = ReferenceSet(Ad.id, ShowRecord.ad_id)
ShowRecord.ad = ReferenceSet(ShowRecord.ad_id, Ad.id)

class ClickRecord(object):
    __storm_table__ = 'click_records'
    id = Int(primary=True)
    
    ad_id = Int()
    keyword_id = Int()
    
    clicked_at = DateTime()
    click_cost = Decimal()
    
    ip = Unicode()
Ad.click_records = ReferenceSet(Ad.id, ClickRecord.ad_id)
ClickRecord.ad = ReferenceSet(ClickRecord.ad_id, Ad.id)

def create_default_campaign():    
    
    c = Campaign()
    
    c.name=u''
    c.description = u''
    c.budget = decimal.Decimal(0)
    c.start_on = datetime.date.today()
    c.end_on = datetime.date.today()
    c.status = u'draft'
    return c

def create_default_ad():
    
    a = Ad()
    #c.user = current_user
    
    a.name=u''
    a.decription = u''
    a.budget = decimal.Decimal(0)
    
    a.headline = u''
    a.headline = u''
    a.desc_line1 = u''
    a.desc_line2 = u''
    a.display_url = u''
    a.destination_url = u''
    
    a.status = u'draft'
    return a

def find_camp(user_name, camp_name):
    import env
    return env.store.find(Campaign, Campaign.name==camp_name, \
               Campaign.user_id==User.id, User.name==user_name).one()

def find_ad(user_name, ad_name):
    import env
    return env.store.find(Ad, Ad.name==ad_name, \
               Ad.user_id==User.id, User.name==user_name).one()
               
if __name__ == '__main__':
    from decimal import Decimal
    from datetime import *
    
